#include "player.h"

Player::Player(void)
{
	this->health = 100;
	this->attackPower = 13;
	this->speed = PLAYER_WALK_SPEED;
	this->currentHeight = 1.0f;
	this->jumping = false;
	this->jumpSpeed = 0.065f;
	initInputFunc();
}

void Player::draw(sf::RenderWindow &window)
{
	this->Sprite.SetPosition(this->xPos, this->yPos);
	this->Sprite.SetCenter(this->Sprite.GetImage()->GetWidth() / 2.0f, 
		this->Sprite.GetImage()->GetHeight() / 2.0f);
	if(jumping)
	{
		this->Sprite.Scale(1/this->currentHeight, 1/this->currentHeight);
		this->currentHeight += this->jumpSpeed;
		if(this->currentHeight >= 2.0f)
		{
			this->jumpSpeed = -this->jumpSpeed;
		}
		if(this->currentHeight <= 0.91f)
		{
			this->jumpSpeed = -this->jumpSpeed;
			this->currentHeight = 1.0f;
			this->jumping = false;
		}
		this->Sprite.Scale(this->currentHeight, this->currentHeight);
	}
	window.Draw(this->Sprite);
}
void Player::initInputFunc()
{
	inputFunctions[1] = &playerRun;
	inputFunctions[2] = &playerJump;
	inputFunctions[3] = &playerStatMenu;

	inputFunctions[10] = &playerUp;
	inputFunctions[11] = &playerDown;
	inputFunctions[12] = &playerLeft;
	inputFunctions[13] = &playerRight;
	inputFunctions[14] = &playerUpRight;
	inputFunctions[15] = &playerDownRight;
	inputFunctions[16] = &playerDownLeft;
	inputFunctions[17] = &playerUpLeft;
	inputFunctions[19] = &nplayerRun;
	inputFunctions[20] = &nplayerJump;
	inputFunctions[21] = &nplayerStatMenu;
}

void playerUp(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->yPos -= player->speed;
}
void playerDown(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->yPos += player->speed;
}
void playerLeft(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->xPos -= player->speed;
}
void playerRight(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->xPos += player->speed;
}
void playerRun(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->speed = PLAYER_RUN_SPEED;
}
void nplayerRun(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->speed = PLAYER_WALK_SPEED;
}
void playerJump(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	if(!player->keysPressed[2])
	{
		player->jumping = true;
		player->keysPressed[2] =  true;
	}
}
void nplayerJump(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	if(player->keysPressed[2])
	{
		player->keysPressed[2] =  false;
	}
}

void playerStatMenu(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	if(!player->keysPressed[3])
	{
		player->keysPressed[3] = true;
	}
}
void nplayerStatMenu(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	if(player->keysPressed[3])
	{
		PlayersExtraData* extra = static_cast<PlayersExtraData*>(extraData);
		extra->statsMenu->menuOn(true);
		player->keysPressed[3] = false;
	}
}
//*******************************//
//For controller use only
//*******************************//
void playerUpRight(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->xPos += player->speed;
	player->yPos -= player->speed;
}
void playerDownRight(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->xPos += player->speed;
	player->yPos += player->speed;
}
void playerDownLeft(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->xPos -= player->speed;
	player->yPos += player->speed;
}
void playerUpLeft(void *object, void *extraData)
{
	Player* player = static_cast<Player*>(object);
	player->xPos -= player->speed;
	player->yPos -= player->speed;
}
//*******************************//
//*******************************//